<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of media_m
 *
 * @author Gidaff_01
 */
class Media_m extends CI_Model {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function get_all_medias($per_page, $off_set) {
        return $this->db->select('media.*, albums.title as album')
                ->from('media')
                ->join('albums', 'media.album_id = albums.id', 'left')
                ->order_by('media.id DESC')
                ->limit($per_page, $off_set)
                ->get()->result_array();
    }

    function count_media() {
        return count($this->db->select('media.*, albums.title as album')
                        ->from('media')
                        
                        ->join('albums', 'media.album_id = albums.id', 'left')
                        ->order_by('media.id DESC')
                        ->get()->result_array());
    }

    function get_all_medias_by_album($album_id, $per_page, $off_set) {
        return $this->db->select('media.*, albums.title as album')
                ->from('media')
                ->where('media.album_id', $album_id)
                ->join('albums', 'media.album_id = albums.id', 'left')
                ->order_by('media.id DESC')
                ->limit($per_page, $off_set)
                ->get()->result_array();
    }

    function count_media_by_album($album_id) {
        return count($this->db->select('media.*, albums.title as album')
                        ->from('media')
                        ->where('media.album_id', $album_id)
                        ->join('albums', 'media.album_id = albums.id', 'left')
                        ->order_by('media.id DESC')
                        ->get()->result_array());
    }

    function add_media($additional_data) {
        $data = array('upload_time' => now());
        $columns = array('file_path', 'upload_time', 'album_id');

        if (!empty($columns)) {
            foreach ($columns as $input) {
                if (is_array($additional_data) && isset($additional_data[$input])) {
                    $data[$input] = $additional_data[$input];
                } elseif ($this->input->post($input)) {
                    $data[$input] = $this->input->post($input);
                }
            }
        }
        return $this->db->insert('media', $data);
    }

    function delete_media($id) {
        return $this->db->delete('media', array('id' => $id));
    }

    function delete_medias($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('media');
        return $query;
    }

    function get_detail_media($id) {
        return $this->db->select('media.*, albums.title as album')
                ->from('media')
                ->where('media.id', $id)
                ->join('albums', 'media.album_id = albums.id', 'left')
                ->order_by('media.id DESC')
                ->get()->row_array();
    }

    function get_media_by_album($album_id) {
        return $this->db->select('media.*, albums.title as album')
                ->from('media')
                ->where('media.album_id', $album_id)
                ->join('albums', 'media.album_id = albums.id', 'left')
                ->order_by('media.id DESC')
                ->get()->result_array();
    }

    function update_media($id, $data) {
              
        return $this->db->update('media', $data, array('id' => $id));
    }

    function get_all_albums($per_page, $off_set) {
        return $this->db->select()
                ->from('albums')
                ->limit($per_page, $off_set)
                ->get()->result_array();
    }
    function get_all_albums_sidebar() {
        return $this->db->select()
                ->from('albums')
                ->get()->result_array();
    }

    function count_album() {
        return count($this->db->select()
                        ->from('albums')
                        ->get()->result_array());
    }

    function add_album($data) {
        return $this->db->insert('albums', $data);
    }

    function update_album($data, $id) {
        return $this->db->update('albums', $data, array('id' => $id));
    }

    function delete_album($id) {
        $this->db->delete('media', array('album_id' => $id));
        return $this->db->delete('albums', array('id' => $id));
    }
    function delete_albums($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('albums');
        $this->db->where('album_id in ' . $list);
        $query1 = $this->db->delete('media');
        return $query;
    }

    function get_album_by_id($id) {
        return $this->db->select()
                ->from('albums')->where('id', $id)
                ->get();
    }
    function get_album_name($id) {
        return $this->db->select('title')
                ->from('albums')->where('id', $id)
                ->get()->row_array();
    }
    
    function get_category_of_album($id){
       $query =  $this->db->select('category_id')
                ->from('albums')
                ->where('id', $id)
                ->get();
       
       return $query->row();
   
    }
    
    function get_image_title($title, $id){
        $query = "SELECT title FROM media WHERE id != '".$id."' AND title = '".$title."'";
        $result = $this->db->query($query);
        return $result;
    }
    
     
    
    function get_photo_albums($per_page, $off_set){
       $query =  "SELECT * FROM albums WHERE EXISTS(SELECT * FROM media WHERE media.album_id = albums.id) LIMIT ".$off_set.", ".$per_page." ";
       $result = $this->db->query($query);
            /*$this->db->select('albums.*, media.id, media.album_id as albumid')
                     ->from('albums')
                     ->join('media', 'media.album_id = albums.id')  
                     ->where('albums.kind', 'image')
                                                               
                     ->limit($per_page, $off_set)
                     ->get()->result_array();   */
          if(count($result) < 1 ){
              return FALSE;
          }else{
              return $result->result_array();
          }       
    }
    
    
    
       
    function count_photo_albums(){
            $query =  "SELECT * FROM albums WHERE EXISTS(SELECT * FROM media WHERE media.album_id = albums.id) ";
            $result = $this->db->query($query);
            return $result->num_rows();
    }
                             
    function list_photo_by_album($id){
        $query =
            $this->db->select('media.*, albums.id')
                     ->from('media')
                     ->join('albums','albums.id = media.album_id')
                     ->where('albums.id', $id)
                     
                     ->get()->result_array(); 
          if(count($query) < 1 ){
              return FALSE;
          }else{
              return $query;
          }   
    }
    
    function list_other_photo_album($old_album_id, $per_page, $off_set){
        
            $query =  "SELECT * FROM albums WHERE EXISTS
            (SELECT * FROM media WHERE media.album_id = albums.id)
             AND albums.kind = 'image' AND albums.id <> $old_album_id
             LIMIT ".$off_set.", ".$per_page."";
            $result = $this->db->query($query);
            return $result->result_array();
    }
    
    function count_other_photo_album($old_album_id){
        $query =
            $this->db->select('albums.*')
                     ->from('albums')
                     ->where('albums.kind', 'image')
                     ->where_not_in('albums.id', array($old_album_id))
                     ->get()->num_rows();
        return $query; 
         
    }
    
    function defaul_photo_of_album($id){
        $query =
            $this->db->select('media.*, albums.id as albumid, albums.kind')
                     ->from('media')
                     ->join('albums','albums.id = media.album_id')
                     ->where('albums.id', $id)
                     ->where('albums.kind', 'image')
                     ->order_by('media.id', 'ASC')
                     ->limit(1)
                     ->get()->row(); 
          if(count($query) < 1 ){
              return FALSE;
          }else{
              return $query;
          }   
    }
    
    function count_photo_in_album($id){
        $query =
            $this->db->select('media.*, albums.id')
                     ->from('media')
                     ->join('albums','albums.id = media.album_id')
                     ->where('albums.id', $id)
                     
                     ->get()->num_rows(); 
         
    }
    
    function get_current_album_total_views($id){
    	$query = $this->db->select("total_views")
    					  ->from("albums")
    					  ->where("id", $id)
    					  ->get();
    	return $query->row();
    					  
    }
    
    function add_home_images($additional_data) {
        
        $columns = array('file_name');

        if (!empty($columns)) {
            foreach ($columns as $input) {
                if (is_array($additional_data) && isset($additional_data[$input])) {
                    $data[$input] = $additional_data[$input];
                } elseif ($this->input->post($input)) {
                    $data[$input] = $this->input->post($input);
                }
            }
        }
        return $this->db->insert('home_image', $data);
    }
    
    function get_home_images(){
        $query = $this->db->select()
                          ->from("home_image")
                          ->order_by("id", "DESC")
    			  ->get();
    	return $query->result_array();
    				
    }
    
    function delete_home_image($id) {
        return $this->db->delete('home_image', array('id' => $id));
    }
    
    function get_detail_home_image($id) {
        return $this->db->select()
                ->from('home_image')
                ->where('id', $id)
                ->get()->row_array();
    }
    
    function delete_multi_home_images($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('home_image');
        return $query;
    }
    
    function add_category($data) {
        return $this->db->insert('album_category', $data);
    }
    
    function count_category() {
        return count($this->db->select()
                        ->from('album_category')
                        ->get()->result_array());
    }
    
    function get_all_category() {
        return $this->db->select()
                ->from('album_category')
                ->get()->result_array();
    }
    
    function list_album_by_category($id){
        $query =
            $this->db->select('albums.*')
                     ->from('albums')
                     ->where('albums.category_id', $id)
                     
                     ->get()->result_array(); 
          if(count($query) < 1 ){
              return FALSE;
          }else{
              return $query;
          }   
    }
    
    function count_album_by_category($category_id){
        
        $query = $this->db->select("albums.id")
    			->from("albums")
    			->where("albums.category_id", $category_id)
    			->get();
    	return $query->num_rows();
    }
    
    function get_all_album_by_category($id, $perpage, $offset){
        $query =
            $this->db->select('albums.*, album_category.id as cat_id, album_category.name as cat_name')
                     ->from('album_category')
                     ->join('albums','albums.category_id = album_category.id')
                     ->where('albums.category_id', $id)
                     ->limit($perpage, $offset)
                     ->get()->result_array(); 
          if(count($query) < 1 ){
              return FALSE;
          }else{
              return $query;
          }   
    }
    
    function count_all_album_by_category($id){
        $query =
            $this->db->select('albums.*, album_category.id as cat_id, album_category.name as cat_name')
                     ->from('album_category')
                     ->join('albums','albums.category_id = album_category.id')
                     ->where('albums.category_id', $id)
                     
                     ->get();
        return $query->num_rows(); 
    }
    
    function get_category_by_id($id) {
        return $this->db->select()
                ->from('album_category')->where('id', $id)
                ->get();
    }
    
    function update_category($data, $id) {
        return $this->db->update('album_category', $data, array('id' => $id));
    }
        
    
    function get_name($table, $field, $id){
        $query = $this->db->select()
    			->from($table)
    			->where($field, $id)
                        ->limit(1)
    			->get();
    	return $query->row();
    }
    
    function delete_category($id) {
        $this->db->delete('albums', array('category_id' => $id));
        return $this->db->delete('album_category', array('id' => $id));
    }
    
    function get_media_category(){
        $query = $this->db->select()
                          ->from("album_category")
                          ->order_by("id", "ASC")
                          ->get();
        return $query->result_array();
    }
    
	function get_image_of_album($album_id){
        $query = $this->db->select()
                          ->from("media")
                          ->where("media.album_id", $album_id)
    			          ->get();
    	return $query->result_array();
    				
    }
    
    function get_name_of_album($id){
    	 $query = $this->db->select("title")
                          ->from("albums")
                          ->where("id", $id)
    			          ->get();
    	return $query->row();	
    }
    
    
    
    

}

?>
